package com.shop.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @version  1.o
 * @author  huangfei
 * controller日志处理类
 */
@Component
@Aspect
@Slf4j
public class AopConfig {

    /**
     * 日志打印切入方法
     */
    @Pointcut("execution(public * com.shop.admincontroller.*.*(..))")
    public void log(){

    }

    @Before("log()")
    public void logBeforeController(JoinPoint joinPoint) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        log.info("请求URL : " + request.getRequestURL().toString());
        log.info("请求方式: " + request.getMethod());
        log.info("请求IP : " + request.getRemoteAddr());
        log.info("请求参数 : " + Arrays.toString(joinPoint.getArgs()));
        log.info("请求类和方法 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

    }


}
